package cqie.cn.frame.config;

import com.auth0.jwt.exceptions.SignatureVerificationException;
import cqie.cn.frame.utils.JWTUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 王林
 * @version 1.0
 * @date 2021-09-10 周五
 * @time 15:21
 * @description
 */
public class JWTInterceptorConfig implements HandlerInterceptor {

//    @Bean
//    public AuthenticationInterceptor authenticationInterceptor() {
//        return new AuthenticationInterceptor();
//    }
//
//    // 这个方法用来注册拦截器，我们自己写好的拦截器需要通过这里添加注册才能生效
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        //注册TestInterceptor拦截器
//        InterceptorRegistration registration = registry.addInterceptor(authenticationInterceptor());
//        registration.addPathPatterns("/**");//所有路径都被拦截
//        registration.excludePathPatterns("" +
//                        "/assets/**",             // assets文件夹里文件不拦截
//                "/**/*.js",              //js静态资源不拦截
//                "/**/*.css"             //css静态资源不拦截
//        );
//    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Map<String, String> map = new HashMap<>();
        String token = request.getHeader("token");
        // 获取请求头中令牌
        try {
            JWTUtils.verify(token);

        } catch (SignatureVerificationException e) {
            e.printStackTrace();
        }


        return false;
    }
}
